var business = function(){
	this.box = $("#formpage");	//表单
	this.input = this.box.find("span.at_text>input");
	this.recvUserId = $("#txtRecvUserid"); //收货方代码
	this.recvUserIdCheck = false;
	this.seqId = $("#seqId");	//封条号
	this.sCheck = false;
	this.carrierNo = $("#carrierNo");//柜号或车牌号
	this.carrierNoCheck = false;
	this.goodsNum = $("#goodsNum");	//数量
	this.goodsNumCheck = false;
	this.goodsInfo = $("#goodsInfo");	//数量
	this.goodsInfoCheck = false;
	
	this.btn = $("#reg_sub");	//提交按钮

	var me = this;  //page  1快速 2邮箱 3手机 4修改密码 5创建安全密码 6修改安全密码 7重设密码 
	
	var isloading=false;
	
	//提示信息
	var h_on = '<em class="on">';	
	var h_err = '<em class="orr"><u class="at_msg m_3_e"></u>';
	var recvUserId_o = h_on+'<span id="namecount">·请选择收货方</span></em>';
	var seqId_o = h_on+'<span id="namecount">·请输入封条号</span></em>';
	var carrierNo_o = h_on+'<span id="namecount">·请输入柜号/车牌号</span></em>';
	var goodsNum_o = h_on+'<span id="namecount">·请输入货物数量</span></em>';
	
	//错误
	var recvUserId_e1 = h_err+'请输入正确的收货方代码</em>';
	var seqId_e1 = h_err+'封条号不能为空</em>';
	var seqId_e2 = h_err+'请输入正确的封条号</em>';
	var carrierNo_e1 = h_err+'柜号或车牌号不能为空</em>';
	var goodsNum_e1 = h_err+'货物数量不能为空</em>';
	var goodsInfo_e1 = h_err+'货物描述不能为空</em>';
	
	//正确
	var name_g = '<em><u class="at_msg m_3_o"></u></em>';
	var name_ld = '<em class="ld"><u class="at_msg at_write"></u>正在检测中……</em>';

	function txt(t){var a=t.parents(".at_text");return a};	
	this.input.focus(function(){//获得焦点
		txt($(this)).attr("class","at_text t_1_i");		
		txt($(this)).find("em").remove();	
		$(this).parents("dd").css("z-index",999);
	})
	this.input.blur(function(){//失去焦点
		txt($(this)).attr("class","at_text t_1_d");
		txt($(this)).find("em").remove();
		$(this).parents("dd");
	})
	var inErr = function(a,b){//错误
		var at_text = txt(a);
		at_text.attr("class","at_text t_1_e");
		at_text.find("em").remove();
		at_text.append(b);
		return false;
	}
	var inOk = function(a){//正确
		var at_text = txt(a);
		at_text.attr("class","at_text t_1_o");
		at_text.find("em").remove();
		at_text.append(name_g);
	}
	var inPass = function(a){//正常，不加“正确”图标
		var at_text = txt(a);
		at_text.attr("class","at_text t_1_o");
		at_text.find("em").remove();
		//at_text.append(name_g);
	}
	
	var namelength = function(a) {//统计字符长度，中文算二个字符
		var name = a.val(),
			length = 0;
		for(var i=0; i<name.length; i++) {
			if(/[\u4e00-\u9fa5]/.test(name.charAt(i))) length+=2;
			else length+=1;
		}
		return length;
	};
	
	var strTrimLength = function(a) {//统计字符长度，不包括空格在内
		var name = a.val(),
			length = 0;
		name=$.trim(name);
		for(var i=0; i<name.length; i++) {
			if(/[\u4e00-\u9fa5]/.test(name.charAt(i))) length+=2;
			else length+=1;
		}
		return length;
	};
	
	var clearEmpty = function(O) {//禁止输入空格
		O.val(O.val().replace(/\s*/g,""));
	}
	
	this.seqIdCheck = function(i,s){//验证封条		
		//var e_nameStr = /[0-9]*[1-9][0-9]*$/.test(i.val());		
		var len = strTrimLength(i);
		if(len==0){
			me.sCheck = inErr(i,seqId_e1);	
			return;
		}
		
		if (len<5) {
			me.sCheck=inErr(i,seqId_e2);
			return;
		}	
		txt(i).attr("class","at_text t_1_l").append(name_ld);
		var params = {
				seqId:i.val()
				};
		$.ajax({type: "POST",
		       url: "ajax_findValid.do",
		       data: params,
		       async: true,
		       success: function(result){
				 var validate=result.validate;
				 if(validate != null){
				  	var status=validate.status;
				  	if(result.retcode == "0000"){//未激活
				  		inOk(i);
						me.sCheck=true;
				  	}else{
				  		me.sCheck=inErr(i,'<em class="orr"><u class="at_msg m_3_e"></u>'+ result.retmsg + '</em>');
				  		me.sCheck=false;
				  	}
				 }else{
				 	me.sCheck=inErr(i,'<em class="orr"><u class="at_msg m_3_e"></u>'+ result.retmsg + '</em>');
				 	me.sCheck=false;
				 }
			  }
			},"json");
	}
	
	this.recvUserIdCheckFun = function(i,s){ //收货人验证
		//$("#recvCompanyid").val("");
		//alert("ggg recvUserIdCheckFun");
		var userIdVal= i.val();
		if(userIdVal==""){
			//$d("recvUsername").innerHTML="请输入收货方账户代码";
			$d("recvUsername").innerHTML="";
			return;
		}
		
		var index=userIdVal.indexOf("-");
		if(index==-1){
			$("#recvUsername").innerHTML="收货方账户代码不正确";
			return;
		}else{
			var strArr=userIdVal.split("-");
			if(strArr.length==2){
				var params = {userIdVal:userIdVal};
				$.ajax({type: "POST",
					   dataType: "json",
				       url: "ajaxUser_checkUserId.do",
				       data: params,
				       async: true,
				       success: function(result){
						 if(result.retcode=="0000"){
						  	$("#recvUsername").innerHTML="账户代码不存在";
						 }else{
						 	$("#recvUsername").innerText = result.companyInfo.companyName;
						 	$("#recvCompanyid").value = result.companyInfo.companyId;
							$("#txtRecvUserid").value = result.userInfo.userId;
						 }
					  }
					},"json");
			}else{
				$d("recvUsername").innerHTML="不存在的收货方";
			}
		}
	}
	
	this.carrierNoCheckFun = function(i,s){//柜号、车牌号	
		var len = strTrimLength(i);
        if (len == 0) {//8-50字符 (1汉字等于2字符)
            me.carrierNoCheck = inErr(i,carrierNo_e1);			
			return;
        }
		inPass(i);				
		me.carrierNoCheck = true;
	}
	
	this.goodsNumCheckFun = function(i,s){//数量		
		var len = strTrimLength(i);		
        if (len == 0) {//8-50字符 (1汉字等于2字符)
            me.goodsNumCheck = inErr(i,goodsNum_e1);			
			return;
        }
		inPass(i);				
		me.goodsNumCheck = true;
	}
	
	this.goodsInfoCheckFun = function(i,s){//数量		
		var len = strTrimLength(i);		
        if (len == 0) {//8-50字符 (1汉字等于2字符)
            me.goodsInfoCheck = inErr(i,goodsInfo_e1);			
			return;
        }
		inPass(i);				
		me.goodsInfoCheck = true;
	}
	
	this.btn.click(function(){//按钮提交
	    if(isloading){return false;}
		if(me.sCheck==false) {
			inPass(me.seqId);
		}
		if(me.carrierNoCheck==false){
			inPass(me.carrierNo);
			me.carrierNoCheckFun(me.carrierNo,false);
		}
		if(me.goodsNumCheck==false){
			inPass(me.goodsNum);
			me.goodsNumCheckFun(me.goodsNum,false);
		}
		if(me.goodsInfoCheck==false){
			inPass(me.goodsInfo);
			me.goodsInfoCheckFun(me.goodsInfo,false);
		}
		if(!me.sCheck || !me.carrierNoCheck || !me.goodsNumCheck || !me.goodsInfoCheck) return false;
		
		return me.box.submit();
	})
	
	
	//重置表单
	var clearForm = function(){
		inPass(me.seqId);
	};
	
	this.recvUserId.focus(function(){
		//if(strTrimLength(me.recvUserId)==0)
			//txt(me.recvUserId).append(recvUserId_o);
		me.recvUserIdCheck=false;
	})
	//延时200毫秒才触发验证
	this.recvUserId.blur(function(){setTimeout(function(){ me.recvUserIdCheckFun(me.recvUserId)},200)});
	
	this.seqId.focus(function(){
		//if(strTrimLength(me.seqId)==0)
		//	txt(me.seqId).append(seqId_o);
		me.sCheck=false;
	})
	//延时200毫秒才触发验证
	this.seqId.blur(function(){setTimeout(function(){ me.seqIdCheck(me.seqId)},200)});
	
	this.carrierNo.focus(function(){
		//if(strTrimLength(me.carrierNo)==0)
		// txt(me.carrierNo).append(carrierNo_o);
		me.carrierNo.isCheck=false;
	})
	this.carrierNo.blur(function(){me.carrierNoCheckFun(me.carrierNo)});
		
	this.goodsNum.focus(function(){
		//if(strTrimLength(me.goodsNum)==0)
		//	txt(me.goodsNum).append(goodsNum_o);
		me.goodsNum.isCheck=false;
	})
	this.goodsNum.blur(function(){me.goodsNumCheckFun(me.goodsNum)});
	
	this.goodsInfo.focus(function(){
		//if(strTrimLength(me.goodsInfo)==0)
		//	txt(me.goodsInfo).append(goodsInfo_o);
		me.goodsInfo.isCheck=false;
	})
	this.goodsInfo.blur(function(){me.goodsInfoCheckFun(me.goodsInfo)});
	
	
	//第一个控件获得焦点
	this.input.eq(0).focus();	
	
	me.box.submit(function(){
	    me.btn.click();
	    return false;
	});
}
